package com.deco.back.controller.product;

import java.util.List;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;

import com.deco.domain.vo.product.PartsInfoImageVo;
import com.deco.domain.vo.product.PartsInfoVedioVo;
import com.free.common.vo.PageResponseVo;
import com.free.common.vo.PageSearchVo;
import com.free.ext.shiro.SessionManager;
import com.free.ext.web.spring.annoation.PageSearch;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.deco.domain.vo.fix.FixGeneralVo;
import com.deco.domain.vo.fix.FixTypeVo;
import com.deco.domain.vo.product.PartsCategoryVo;
import com.deco.domain.vo.product.PartsInfoVo;
import com.deco.service.fix.FixGeneralService;
import com.deco.service.fix.FixTypeService;
import com.deco.service.product.PartsCategoryService;
import com.deco.service.product.PartsInfoService;
import com.free.common.vo.ResultModel;

import org.springframework.web.servlet.ModelAndView;

/**
 * 广告管理 Controller
 * @author lengy
 *
 */
@Controller
public class PartsInfoController {
    @Autowired
    private PartsInfoService partsInfoService;

    @Autowired
    private PartsCategoryService partsCategoryService;
    
    @RequestMapping(value = "/parts_info_list.do")
    public ModelAndView listPage(){
        ModelAndView mv = new ModelAndView("prod/partsinfolist");
        List<PartsCategoryVo> categoryList =  partsCategoryService.getAll();
        mv.addObject("categoryList", categoryList);
        return mv;
    }

    @RequestMapping(value= "/parts_info_list_page.do")
    @ResponseBody
    public PageResponseVo<PartsInfoVo> getList(@PageSearch PageSearchVo<PartsInfoVo> condition){
        return partsInfoService.pageInfo(condition);
    }

    @RequestMapping(value = "/parts_info_edit.do")
    public ModelAndView editPage(Integer id){
    	PartsInfoVo vo = null;
        ModelAndView mv = new ModelAndView("prod/partsinfoedit");
        List<PartsCategoryVo> categoryList =  partsCategoryService.getAll();
        mv.addObject("categoryList", categoryList);
        Integer maxSort = partsInfoService.getMaxSort();
        mv.addObject("maxSort", maxSort);
        if(id != null){
            vo = partsInfoService.getPartsInfo(id);
            StringBuffer strBuf = new StringBuffer();
            int size = 1;
            if(vo.getImageList() != null && vo.getImageList().size()>0){
                for(PartsInfoImageVo imgVo : vo.getImageList()){
                    if(strBuf.length() == 0){
                        strBuf.append(size).append(":").append(imgVo.getImageUrl());
                    }else{
                        strBuf.append(",").append(size).append(":").append(imgVo.getImageUrl());
                    }
                    size++;
                }
                mv.addObject("imageUrls",strBuf.toString());
            }
            if(vo.getVedioList() != null && vo.getVedioList().size() >0){
                PartsInfoVedioVo vedioVo = vo.getVedioList().get(0);
                mv.addObject("viewImageUrl",vedioVo.getImageUrl());
                mv.addObject("vedioUrl",vedioVo.getVedioUrl());
            }

            mv.addObject("actNm","更新");
        }else{
            vo = new PartsInfoVo();
            vo.setId(0);
            mv.addObject("actNm","新增");
        }
        mv.addObject("obj",vo);

        return mv;
    }

    @RequestMapping(value ="/parts_info_del.do")
    @ResponseBody
    public ResultModel batchDelete(@RequestParam(value = "ids[]")Integer[] ids){
        return partsInfoService.batchDelete(ids);
    }

    @RequestMapping(value ="/parts_info_edit_submit.do")
    @ResponseBody
    public ResultModel udpate(PartsInfoVo vo, String[] imageList, String vedioUrl, String vedioImgUrl){
        return partsInfoService.save(vo);
    }

    @RequestMapping(value ="/parts_info_status.do")
    @ResponseBody
    public ResultModel updateStatus(PartsInfoVo vo){
        return partsInfoService.save(vo);
    }

}
